package com.stars.services.impl.system;
import com.stars.common.base.ResultMsg;
import com.stars.common.valid.MyException;
import com.stars.entity.dto.system.SysMenuUrlsDTO;
import com.stars.entity.system.SysMenuUrlsEntity;
import com.stars.services.system.SysMenuUrlsConfigServices;
import com.stars.dao.mapper.system.SysMenuUrlsMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.stars.services.impl.BaseConfigServiceImpl;
/**
 * @Title: 菜单与url的关系表 业务逻辑
 * @author dj
 * @date 2019-10-23 20:48:11
 * @version V1.0
 *
 */
@Service
public class SysMenuUrlsConfigServicesImpl  extends BaseConfigServiceImpl<SysMenuUrlsMapper , SysMenuUrlsEntity> implements SysMenuUrlsConfigServices {
    @Autowired
    private SysMenuUrlsMapper sysMenuUrlsMapper;
    @Override
    public ResultMsg add(String menuId, String[] urlIds) throws Exception {
        checkAdd(menuId,urlIds);
        for (String perId : urlIds) {
            SysMenuUrlsEntity sysMenuUrlsEntity = new SysMenuUrlsEntity();
            sysMenuUrlsEntity.setMenuId(menuId);
            sysMenuUrlsEntity.setUrlsId(perId);
            insert(sysMenuUrlsEntity);
        }
        return ResultMsg.success();
    }

    @Override
    public ResultMsg delByIdAndUrlId(String menuId, String[] urlIds) throws Exception {
        checkAdd(menuId,urlIds);
        for (String perId : urlIds) {
            SysMenuUrlsDTO dto = new SysMenuUrlsDTO(menuId,perId);
            sysMenuUrlsMapper.delByMenuIdAndUrlId(dto);
        }
        return ResultMsg.success();
    }

    /**
     * 校验数据
     * @param id
     * @param perIds
     */
    private void checkAdd(String id,String [] perIds){
        if(StringUtils.isBlank(id)){
            throw new MyException("没有选择添加的菜单");
        }
        if(null == perIds){
            throw new MyException("没有选择关联的URL");
        }
    }
}